package com.bj.dz.controller;

import com.bj.dz.ResultModel.R;
import com.bj.dz.ResultModel.RHelper;
import com.bj.dz.ResultModel.ResponseCode;
import com.bj.dz.entity.Users;
import com.bj.dz.service.UsersService;
import com.bj.dz.util.MobileUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/***
*   用户控制器
*/
@Slf4j
@Api(tags = "【用户】提供服务接口")
@RestController
@RequestMapping("/users")
public class UsersController {

    @Resource
    private UsersService usersService;


    /**
     * 登录验证
     * @param
     * @return
     */
    @ApiOperation(value = "登录服务【使用】")
    @RequestMapping(value="/checkLogin",method = RequestMethod.POST, produces = "application/json;" +"charset=UTF-8")
    public R checkLogin(@ApiParam(name = "phone",value = "用户的电话",required = true) @RequestParam(value = "phone",required=true)String phone,
                        @ApiParam(name = "userName",value = "用户名称",required = true) @RequestParam(value = "userName",required=true)String userName){
        //设置返回结果
        R r= RHelper.ok();
        //验证手机号的有效性
        boolean flag= MobileUtil.isMobile(phone);
        if(flag){
            //验证手机号和用户是否存在
            Map<String,Object> params=new HashMap<String,Object>();
            params.put("phone",phone);
            params.put("userName",userName);
            List<Users> usersList=usersService.getUsersListByMap(params);
            log.info(usersList.size()+"集合长度.......");
            if(usersList!=null && !usersList.isEmpty()){
                log.info("【登录】用户名和手机号存在.............【登录】");
                Users user=usersList.get(0);
                log.info("【用户名】"+user.getUserName()+"【用户名】");
                //session.setAttribute("user",usersList.get(0));
                r.setMsg("登录成功");
                r.setDatas(user);
                return r;
            }else{
                r.setCode(ResponseCode.FAILED_CODE);
                r.setMsg("用户名或手机号错误,请检查");
            }
        }else {
            r.setCode(ResponseCode.FAILED_CODE);
            r.setMsg("手机号不正确");
        }
        return r;
    }

    /**
    * 通过id查询
    * @param
    * @return
    */
    @ApiOperation(value = "查找服务")
    @RequestMapping(value="/findById",method = RequestMethod.POST, produces = "application/json;" +"charset=UTF-8")
    public Users findUsersById(@ApiParam(name = "id",value = "Users的id",required = true) @RequestParam(value = "id",required=true)Integer id){
        return usersService.getUsersById(id);
    }

    /**
    * 通过多个查询
    * @param
    * @return
    */
    @ApiOperation(value = "查找多个服务")
    @RequestMapping(value="/findUserss",method = RequestMethod.POST, produces = "application/json;" +"charset=UTF-8")
    public List<Users> findUsersListByMap(@ApiParam(name = "Users",value = "users",required = true)@RequestBody Users users){

        return usersService.getUsersListByMap(null);
    }

    /**
    * 查询总数
    * @param
    * @return
    */
    @ApiOperation(value = "获取总数服务")
    @RequestMapping(value="/count",method = RequestMethod.POST, produces = "application/json;" +"charset=UTF-8")
    public Integer UsersCount(){
        return usersService.getUsersCountByMap(null);
    }

    /**
    * 添加保存
    * @param
    * @return
    */
    @ApiOperation(value = "添加服务")
    @RequestMapping(value="/add",method = RequestMethod.POST, produces = "application/json;" +"charset=UTF-8")
    public boolean addUsers(@ApiParam(name = "Users",value = "users对象",required = true)@RequestBody Users users){
            return usersService.addUsers(users);
    }

    /**
    * 更新修改
    * @param
    * @return
    */
    @ApiOperation(value = "更新修改服务")
    @RequestMapping(value="/upd",method = RequestMethod.POST, produces = "application/json;" +"charset=UTF-8")
    public boolean modifyUsers(@ApiParam(name = "Users",value = "users对象",required = true)@RequestBody Users users){
        return usersService.modifyUsers(users);
    }

    /**
    * 删除
    * @param
    * @return
    */
    @ApiOperation(value = "删除服务")
    @RequestMapping(value="/del",method = RequestMethod.POST, produces = "application/json;" +"charset=UTF-8")
    public boolean deleteUsersById(@ApiParam(name = "Users",value = "users的id",required = true) @RequestParam(value = "id",required=true)Integer id){
        return usersService.deleteUsersById(id);
    }



}
